<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('购买界面')" />
    <style type="text/css">
    #bootstrap-table tr{
    margin: 4vh;
    
    }
        #bootstrap-table th{
    margin-bottom: 2vh;
    
    }
    </style>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>充值单号：</label>
                                <input type="text" name="chargeId"/>
                            </li>
                            <li>
                                <label>vip账号：</label>
                                <input type="text" name="vipId"/>
                            </li>
                               <li>
                                <label>vip用户名</label>
                                <input type="text" name="userName"/>
                            </li>
                            <li>
                                <label>充值金额：</label>
                                <input type="text" name="amount"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
              
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:purchaseview:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
              
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:purchaseview:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <script type="text/javascript">
              
    		    function purchase(){
    				var s="#bootstrap-table tr";
    				var list=$(s);
					map={};
				    var model=document.querySelector("#myModal")
				    var vipId=model.querySelector('input[name="vipId"]').value;
				    if(vipId==null||vipId==undefined)
				    	{
				    	alert("请输入正确的卡号！！！")
				    	}
				    else{
				    	map["vipId"]=vipId;
					for(var i=0;i<list.length;i++)
					{
						var element=list[i];
						var n1=element.querySelector('td input[name="barCode"]');
						var n2=element.querySelector('td input[name="sellQuantity"]');
				      
						if(n1!=undefined&&n2!=undefined&&n1!=null&&n2!=null)
						{
							map[n1.value]=n2.value;
						}
					}
					console.log(prefix+"/purchase");
					$.ajax({
					    type: 'POST',
					    url: prefix+"/purchase", 
					    data: JSON.stringify(map), 
					    contentType: 'application/json', 
					    success: function(response) {
					        console.log('成功发送数据到Controller', response);
					        if(response=="purchaseDefeat")
					        	{
					        	alert("余额不足或者卡号不正确");
					        	 location.reload();
					        	}
					        else if(response=="purchaseSucceed")
					        	{
					        	alert("购买成功");
					        	 location.reload();
					        	}
					    },
					    error: function(xhr, status, error) {
					        console.error('发送数据失败', error);
					    }
					});
				    }
    			}
    		    function turnOffOrUp(barCode,condition)
    		    {
    		    	$.ajax({
					    type: 'POST',
					    url: prefix+"/"+condition, 
					    data: JSON.stringify(barCode), 
					    contentType: 'application/json', 
					    success: function(response) {
					      if(condition=="true")
					    	  {
					    	  alert("上架成功");
					    	  location.reload();
					    	  }
					      else {
					    	  alert("下架成功");
					    	  location.reload();
					      }
					    },
					    error: function(xhr, status, error) {
					        console.error('发送数据失败', error);
					    }
					});
    		    }
                </script>
                <!--   <a class="btn btn-warning"  shiro:hasPermission="system:purchaseview:export">
                    <i class="fa fa-download"></i> 购买
                </a> -->
                <!-- 激活按钮 -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
  结算
</button>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    


<!-- 模态框 -->
<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <div class="modal-header">
        <h4 class="modal-title">请输入卡号</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- 模态框主体 -->
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="cardNumber">卡号：</label>
            <input type="text" class="form-control" name="vipId" placeholder="输入卡号">
          </div>
        </form>
      </div>

      <!-- 模态框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-success" data-dismiss="modal" onclick="purchase()">确认</button>
        <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
      </div>

    </div>
  </div>
</div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:purchaseview:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:purchaseview:remove')}]];
        var prefix = ctx + "system/purchaseview";
        
        $(function() {
            var options = {
                url: prefix + "/list",
                updateUrl: prefix + "/edit/{id}",
              
                exportUrl: prefix + "/export",
                modalName: "购买界面",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'picturePath',
                    title: '商品图片',
                    formatter: function(value, row, index) {
                		return $.table.imageView(value);
                	}
                },
                {
                    field: 'vipId',
                    title: '商品介绍',
                    	formatter: function(value, row, index) {
                    		var actions = [];
                    		actions.push(`<h2>`+row['name']+`</h2>`);
                    		if(row['condition']=="false")
                    			{
                    			actions.push(`<h4 style="color: red;">当前已下架</h4>`);
                    			}
                    		else if(row['quantity']<=0)
                    			{
                    			actions.push(`<h4 style="color: red;">当前已售空了</h4>`);
                    			}
                    		else{
                    		actions.push(`<h5>`+row['quantity']+`</h5>`);
                    		}
                    		actions.push(`<p>暂时对`+row['name']+`还没有任何介绍,现在是来凑字数的hhhh`);
                    		 return actions.join('');
                    	}
                },
                {
                    field: 'barCode',
                    title: '商品条码',
                    visible:false
                },
                {
                    field: 'inportPrice',
                    title: '商品进价',
                    visible:false
                },
                {
                    field: 'price',
                    title: '售价'
                },
                {
                    title: '当前购买数量',     
                formatter: function(value, row, index) {
                	var actions = [];
                	actions.push(`<input type="hidden" name="barCode" value=`+row['barCode']+`>`);
                	if(row['condition']=="false")
        			{
                        actions.push(`<input type="number" name="sellQuantity" value="0" max="0" min="0">`);
        			}
                	else{
                        actions.push(`<input type="number" name="sellQuantity" value="0" max="`+row['quantity']+`" min="0">`);

                	}
                    return actions.join('');
                }
                } ,
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var userRole = '[[${userRole}]]';
                        console.log(userRole);
                        if(userRole != 2){
                            actions.push(`<a class="btn btn-success btn-xs ` + editFlag + ` " href="javascript:void(0)"
                        		onclick="turnOffOrUp(`+ row.barCode+`,'false')"><i class="fa fa-edit"></i>下架</a> `);
                            actions.push(`<a class="btn btn-success btn-xs ` + editFlag + ` " href="javascript:void(0)"
                        		onclick="turnOffOrUp(`+ row.barCode+`,'true')"><i class="fa fa-edit"></i>上架</a> `);
                        }
                       return actions.join('');
                    }
                } ]
            };

            $.table.init(options);
        });
    </script>
</body>
</html>